/*
* ============================================================================
* 功能描述 ：应用层配置
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <stdio.h>

#if 0
static void _temp_handle(signed short t, signed short h)
{
	unsigned char t10 = 0, t100 = 0, t1000 = 0;
	unsigned char h10 = 0, h100 = 0, h1000 = 0;
	
	t10 	= (t/10%10);		// 十位位
	t100 	= (t/100%10);		// 百位
	t1000 	= (t/1000);			// 千位
	DelayXms(2);
//	t_10 	= _temp_lcd(t10);
//	t_100 	= _temp_lcd(t100);
//	t_1000 	= _temp_lcd(t1000);

	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	j  = j1*256+j2;

	h10		= (j/100%10); 		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10);		// wan位
	DelayXms(2);
//	h_10	= _temp_lcd(h10);
//	h_100	= _temp_lcd(h100);
//	h_1000	= _temp_lcd(h1000);

//	hp[0] = SHORT_GET_HIGH_BYTE(h_10);
//	hp[1] = SHORT_GET_LOW_BYTE(h_10);
//	hp[2] = SHORT_GET_HIGH_BYTE(h_100);
//	hp[3] = SHORT_GET_LOW_BYTE(h_100);
//	hp[4] = SHORT_GET_HIGH_BYTE(h_1000);
//	hp[5] = SHORT_GET_LOW_BYTE(h_1000);
	if(t1000 == 1)
	{
		if(t100 <= 8 )
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else if(t1000 == 2)
	{
		if(t100 <= 8)
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else
		Write_Lcd_Storage(face_state,0);
	
#if 0
	char a1[2],a2[2],a3[2];
	a1[0] = h10;
	a2[0] = h100;
	a3[0] = h1000;
	DelayXms(200);
	Uart_Tx_buf(a1, 1);		// 打印每一位值
	DelayXms(200);
	Uart_Tx_buf(a2, 1);
	DelayXms(200);
	Uart_Tx_buf(a3, 1);
#endif
	//Lcd_Set_temp(t_1000, t_100, t_10);
	Write_Lcd_Storage(temp_1,t10);
	DelayXms(2);
	Write_Lcd_Storage(temp_10,t100);
	DelayXms(2);
	Write_Lcd_Storage(temp_100,t1000);
	DelayXms(2);
	Write_Lcd_Storage(hum_10,h100);
	DelayXms(2);
	Write_Lcd_Storage(hum_100,h1000);
	Seg_Set_Data();
}
#endif
static void _temp_handle(signed short t, signed short h)
{
    // 提取温度和湿度的位值
    
	long  j,j1,j2;
    unsigned char t10 = t / 10 % 10, t100 = t / 100 % 10, t1000 = t / 1000;
    unsigned char h10, h100, h1000;
	j1	= SHORT_GET_HIGH_BYTE(h);
	j2	= SHORT_GET_LOW_BYTE(h);
		
	j  = j1*256+j2;
	
	h10 	= (j/100%10);		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10); 	// wan位

   

   
	 // 显示温度和湿度
 
}
void Hly_Temp_Hum_Obtain(void)
{
    signed short t, h;
	char i;
	signed short ak_47;
    long  j,j1,j2;
    unsigned char Data_f;


    if (Data_f == 1) {
	j1	= SHORT_GET_HIGH_BYTE(h);
	j2	= SHORT_GET_LOW_BYTE(h);
	j  = j1*256+j2;
	ak_47	= (j/10000%10); 	// wan位

	if(ak_47 > 0)
		j = j/10;
    // 发送温湿度数据
    Hly_Packet_device_manage(Report_specified_type, 6, t, 0);
    DelayXms(2);
    Hly_Packet_device_manage(Report_specified_type, 6, j, 1);


    // 处理温湿度的每一位值
    _temp_handle(t, h);

    // 注释掉未使用的变量和发送代码，可以考虑移除它们
    // temp[0]  =  SHORT_GET_HIGH_BYTE(t);
    // temp[1]  =  SHORT_GET_LOW_BYTE(t);
    // hum[0]	 =  SHORT_GET_HIGH_BYTE(h);
    // hum[1]	 =  SHORT_GET_LOW_BYTE(h);
    
    // DelayXms(200);
    // Uart_Tx_buf(temp, 2);
    // Uart_Tx_buf(hum, 2);
    }
}

void Hly_Adc_update(void)
{
	double vol_dot;
	int vol,vol_var,vol_avg;
	char i,ch[5];
	vol_avg = Read_ADC(14, 1);
	for(i = 0; i > 10; i++)
	{
		vol = Read_ADC(14, 1);
		vol_avg = (vol + vol_avg)/2;
	}
	ch[0] = SHORT_GET_HIGH_BYTE(vol_avg);
	ch[1] = SHORT_GET_HIGH_BYTE(vol_avg);
	
	vol_dot = (vol_avg/4095)*4;
	vol_var = vol_dot*100;
	ch[2] = SHORT_GET_HIGH_BYTE(vol_var);
	ch[3] = SHORT_GET_HIGH_BYTE(vol_var);


	Uart_Tx_buf(ch,  4 );
	if(vol_var <= 250)
	{
	//	Write_Lcd_Storage(vol_state, 0);		
	}
	if(vol_var > 250)
	{
	//	Write_Lcd_Storage(vol_state, 1);		
	}
//	Seg_Set_Data();
}

void App_Run(void)
{
	Zigbee_Cmd_handle();
	Zigbee_Config_Set();

}

void app_init(void)
{
	Hly_Packet_Network_msg(query_current_network_status, 0, 0);
	DelayXms(10);
//	Hly_Temp_Hum_Obtain();
}

